/**
 * 
 */
package com.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Properties;

/**
 * @author Ltz
 */
public class FileUtil {

	/**
	 * 创建java文件
	 * 
	 * @throws IOException
	 */
	public static boolean creatTxtFile(String packagePath, String fileName, String fileType, String fileContent) throws IOException {
		boolean flag = false;
		packagePath = getConfigFilePath() +"main/java/" + packagePath + "/";
		packagePath = packagePath.replace(".", "/");
		flag = createFolder(packagePath);

		String filenameTemp = packagePath + fileName + fileType;
		File filename = new File(filenameTemp);
		if (!filename.exists()) {
			filename.createNewFile();
			flag = true;
		}
		if (flag) {
			flag = writeTxtFile(fileContent, filenameTemp);
		}
		return flag;
	}

    /**
     * 创建资源文件
     *
     * @throws IOException
     */
    public static boolean creatResFile(String packagePath, String fileName, String fileType, String fileContent) throws IOException {
        boolean flag = false;
        packagePath = getConfigFilePath() +"main/resources/"+ packagePath + "/";
        packagePath = packagePath.replace(".", "/");
        flag = createFolder(packagePath);

        String filenameTemp = packagePath + fileName + fileType;
        File filename = new File(filenameTemp);
        if (!filename.exists()) {
            filename.createNewFile();
            flag = true;
        }
        if (flag) {
            flag = writeTxtFile(fileContent, filenameTemp);
        }
        return flag;
    }
    /**
     * 创建页面文件
     *
     * @throws IOException
     */
    public static boolean creatPagesFile(String packagePath, String fileName, String fileType, String fileContent) throws IOException {
        boolean flag = false;
        packagePath = getConfigFilePath() +"main/webapp/"+ packagePath + "/";
        packagePath = packagePath.replace(".", "/");
        flag = createFolder(packagePath);

        String filenameTemp = packagePath + fileName + fileType;
        File filename = new File(filenameTemp);
        if (!filename.exists()) {
            filename.createNewFile();
            flag = true;
        }
        if (flag) {
            flag = writeTxtFile(fileContent, filenameTemp);
        }
        return flag;
    }

	/**
	 * 写文件
	 * 
	 * @param newStr 新内容
	 * @throws IOException
	 */
	public static boolean writeTxtFile(String newStr, String filenameTemp) throws IOException {
		// 先读取原有文件内容，然后进行写入操作
		boolean flag = false;
		String filein = newStr + "\r\n";

		FileInputStream fis = null;
		InputStreamReader isr = null;
		BufferedReader br = null;

		FileOutputStream fos = null;
		PrintWriter pw = null;
		try {
			// 文件路径
			File file = new File(filenameTemp);
			// 将文件读入输入流
			fis = new FileInputStream(file);
			isr = new InputStreamReader(fis);
			br = new BufferedReader(isr);
			StringBuffer buf = new StringBuffer();
			buf.append(filein);
			fos = new FileOutputStream(file);
			pw = new PrintWriter(fos);
			pw.write(buf.toString().toCharArray());
			pw.flush();
			flag = true;
		} catch (IOException e1) {
			throw e1;
		} finally {
			if (pw != null) {
				pw.close();
			}
			if (fos != null) {
				fos.close();
			}
			if (br != null) {
				br.close();
			}
			if (isr != null) {
				isr.close();
			}
			if (fis != null) {
				fis.close();
			}
		}
		return flag;
	}

	/**
	 * 获取配置文件路径
	 * 
	 * @return
	 */
	public static String getConfigFilePath() {
		Properties prop = new Properties();
		InputStream in = Object.class.getResourceAsStream("/config.properties");
		String rs = null;
		try {
			prop.load(in);
			rs = prop.getProperty("projectPath").trim();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return rs;
	}

	/**
	 * 创建 文件夹
	 * 
	 * @param FolderPath
	 * @return
	 */
	public static boolean createFolder(String FolderPath) {
		File file = new File(FolderPath);
		// 如果文件夹不存在则创建
		if (!file.exists() && !file.isDirectory()) {
			file.mkdirs();
		}
		return true;
	}
}
